package com.example.service.impl.shangPinServiceImpl;

import com.example.dao.shangPinDao.JsProductCategroyDao;
import com.example.pojo.shangPinPojo.JsProductCategroy;
import com.example.pojo.shangPinTree.Node;
import com.example.service.shangPinService.JsProductCategroyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class JsProductCategroyServiceImpl implements JsProductCategroyService {
    @Autowired
    private JsProductCategroyDao jdao;
    /**
     * @Author: 郭
     * @Date: 2022/12/7 11:03
     * @Param:
     * @Return: List<Node>
     * 商品分类表中的全查树形结构，通过递归调用子节点
     */
    @Override
    public List<Node> getAll() {
        List<JsProductCategroy> all = jdao.getAll();
        List<Node> tree = getTree(0,all);
        return tree;
    }

    @Override
    public int getCount() {
        return jdao.getCount();
    }
    //商品分类的树形结构
    public List<Node> getTree(int categrayId,List<JsProductCategroy> list){
        List<Node> objects = new ArrayList<>();
        for (JsProductCategroy categroy : list) {
            if (categroy.getParentId() == categrayId){
                Node node = new Node();
                node.setId(categroy.getCategrayId());
                node.setLabel(categroy.getTitle());
                node.setSpread(true);
                List<Node> tree = getTree(categroy.getCategrayId(),list);
                node.setChildren(tree);
                objects.add(node);
            }
        }
        return objects;
    }

}
